/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "jumpinstructionlogic.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "messagelog.h"


JumpInstructionLogic::JumpInstructionLogic(int robotIdIn,ProgramSet* programSetIn,
                                           RobotIo* ioControllerIn,ProgramEngine* programEngineIn)
{
    programSet=programSetIn;
    robotId=robotIdIn;
    ioController=ioControllerIn;
    programEngine=programEngineIn;
}

E_PROGRAM_LOOP_RESULT JumpInstructionLogic::startJump(int programNum, QString jumpLabelStrIn)
{
//    qDebug() << __FUNCTION__<<"***line***"<<__LINE__<<"==r="<<programSet->r[0]<<"===="<<programSet->r[1]<<"==="<<programSet->r[2]<<endl;
    programEngine->updateProgramSetRegister();

    if( programEngine->getRunModel()<ENUM_CONTROL_AUTO && programEngine->getMoveType()==ENUM_MOVE_ORDER_STEP)
    {
        programEngine->set_mamual_run_flag(false);
    }
    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;
    QString tmpJumpNum;
    for(int i = 0; i < (*programSet)[programNum].count();i++)
    {
        if((*programSet)[programNum].at(i).type == LABEL)
        {
            tmpJumpNum = (*programSet)[programNum].at(i).label.labelStr;
            if(jumpLabelStrIn == tmpJumpNum)
            {
                (*programSet)[programNum].runningLineInt = i;
                programEngine->setRunningNumber_virtualLineControl(i);
                programEngine->setRunningNumber_robotArmControl(i);
                programEngine->setPcFlagValue(0.0);
                break;
            }
            else
            {
                if(i == (*programSet)[programNum].count() - 1)//查找到最后一行，发现没有与之匹配的ｌａｂｅｌ标签，报错处理
                {

                 //   MessageLog::getInstance()->addMessage( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003, robotId );
                    addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003,robotId,0,0,0,0,jumpLabelStrIn );
                    programEngine->cmdError(programNum);
                    qDebug()<<"error:  没找到匹配的ｌａｂｅｌ1";
                    return E_PROGRAM_LOOP_RESULT_ERROR;
//                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        else
        {
            if( i == (*programSet)[programNum].count() - 1)
            {

             //   MessageLog::getInstance()->addMessage( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003, robotId );
                addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003,robotId,0,0,0,0,jumpLabelStrIn );
                programEngine->cmdError(programNum);
                qDebug()<<"error:  没找到匹配的ｌａｂｅｌ2";
                return E_PROGRAM_LOOP_RESULT_ERROR;
            }
        }
    }

    endFlag = programEngine->isTheLastInstruction(programNum);

    return endFlag;

    //    qDebug() << "startJump*****2222222222***programSet.pc"<<(*programSet)[programNum].pc<<"programNum***********"<<programNum<<endl;

}


void JumpInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {

    case 3003:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr(",跳转错误，没有搜索到标签[%1]!").arg(message);
    }
     break;



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
